//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.ContractInfo;
import com.aidong.model.wills.ContractSign;
import com.aidong.model.wills.MemberChangeCard;
import com.aidong.model.wills.PayList;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class MemberChangeCardDao extends BaseDao<MemberChangeCard, Integer> {
	public MemberChangeCardDao() {
	}

	public List<MemberChangeCard> getListByMemberCode(String memberCode) {
		return this.queryList("memberCode=:p1", new Object[]{memberCode}, (new OrderSet()).desc("saleDate"), -1);
	}

	public void queryPageList(Pagination<ContractInfo> pg, ConditionMap cd) {
		ArrayList params = new ArrayList();
		String sqlw = cd.toSql(params);
		String sql = "select c.saleId, c.shopCode, c.saleDate, c.contractCode, c.memberCode, i.chName memberName, c.cardCode\n, c.status, c.addFee, c.payAmount, k.beginTime, k.endTime, k.cardType, c.newCardCode, c.salesCode, c.salesCode2\nfrom MemberChangeCard c inner join MemberContract k on c.contractCode=k.contractCode\ninner join MemberInfo i on c.memberCode=i.memberCode\n" + (sqlw.length() > 0?"where " + sqlw:"") + " order by c.saleDate desc";
		this.queryPageListBySql(ContractInfo.class, pg, sql, params.toArray());
	}

	public void addChangeCard(MemberChangeCard changeCard, List<PayList> payList) {
		this.add(changeCard);
		Iterator sql = payList.iterator();

		while(sql.hasNext()) {
			PayList memberName = (PayList)sql.next();
			memberName.setSaleId(changeCard.getSaleId());
			memberName.setContractCode(changeCard.getContractCode());
			this.getSession().save(memberName);
			if("S011".equalsIgnoreCase(memberName.getPayType())) {
				String sign = "update PayAli set status=:p1, payListId=:p2, salesCode=:p3, salesCode2=:p4, updateTime=:p5 where barCode=:p6";
				this.updateBySql(sign, new Object[]{Integer.valueOf(2), memberName.getAutoId(), memberName.getSalesCode(), memberName.getSalesCode2(), new Date(), memberName.getNote()});
			}
		}

		String sql1 = "update MemberCard set memberCode=:p1, contractCode=:p2, status=2, useUser=:p3, useDate=getdate() where cardCode=:p4";
		this.updateBySql(sql1, new Object[]{changeCard.getMemberCode(), changeCard.getContractCode(), changeCard.getCreateUser(), changeCard.getNewCardCode()});
		sql1 = "update MemberInfo set cardCode=:p1, updateTime=:p2 where memberCode=:p3";
		this.updateBySql(sql1, new Object[]{changeCard.getNewCardCode(), new Date(), changeCard.getMemberCode()});
		if(changeCard.getEarnestId() != null) {
			sql1 = "update MemberEarnest set status=1, useSaleId=:p1, useDate=getdate() where saleId=:p2";
			this.updateBySql(sql1, new Object[]{changeCard.getSaleId(), changeCard.getEarnestId()});
		}

		String memberName1 = (String)this.getBySql(String.class, "select chName from MemberInfo where memberCode=:p1", new Object[]{changeCard.getMemberCode()});
		ContractSign sign1 = new ContractSign();
		sign1.setShopCode(changeCard.getShopCode());
		sign1.setContractCode(changeCard.getContractCode());
		sign1.setContractType("07");
		sign1.setSaleId(changeCard.getSaleId());
		sign1.setMemberCode(changeCard.getMemberCode());
		sign1.setMemberName(memberName1 == null?"":memberName1);
		sign1.setStatus(Integer.valueOf(1));
		sign1.setSalesCode(changeCard.getSalesCode());
		sign1.setSalesCode2(changeCard.getSalesCode2());
		sign1.setCreateUser(changeCard.getCreateUser());
		sign1.setCreateTime(new Date());
		sign1.setUpdateTime(new Date());
		this.getSession().save(sign1);
	}
}
